package com.klei.controller;

import com.klei.pojo.Orders;
import com.klei.pojo.User;
import com.klei.service.ICarService;
import com.klei.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private ICarService carService;

    @GetMapping("/getOrderByUid")
    @ResponseBody
    public List<Orders> getOrderByUid(HttpServletRequest servletRequest){

        return orderService.getOrderByUid(((User)servletRequest.getSession().getAttribute("user")).getUid());
    }

    @PostMapping("/saveOrder")
    @ResponseBody
    public boolean saveOrder(@RequestBody Orders order){

        //预定成功后，车数量减一
        orderService.saveOrder(order);
        boolean flag = carService.updateCarNumber(order.getCid());

        return flag;
    }

}
